/*
 *所有关于Ls_pro_bom类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Ls_pro_bomService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyErp.IRepositories;
using System.Collections.Generic;
using System;
using VOL.Core.DBManager;

namespace MyErp.Services
{
    public partial class Ls_pro_bomService
    {
        private const int V = 0;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ILs_pro_bomRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public Ls_pro_bomService(
            ILs_pro_bomRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        public override PageGridData<Ls_pro_bom> GetPageData(PageDataOptions options)
        {
            //批量更新BOM状态，判断在pdb表是否存在
            List<Ls_pro_bom> ls_Pro_Boms = DBServerProvider.DbContext.Set<Ls_pro_bom>().ToList();
            List<Ls_pdb> Ls_pdbs = DBServerProvider.DbContext.Set<Ls_pdb>().ToList();
            for (int i = 0; i < ls_Pro_Boms.Count; i++)
            {
                try//如果为空说明也没匹配到
                {
                    ls_Pro_Boms[i].isinpdb = Convert.ToInt16(Ls_pdbs.Exists(x => x.matnr.Trim() == ls_Pro_Boms[i].mara_for_ls.Trim()));
                }
                catch (NullReferenceException)
                {
                    ls_Pro_Boms[i].isinpdb = 0;
                }
                
                DBServerProvider.DbContext.Set<Ls_pro_bom>().Update(ls_Pro_Boms[i]);
            }
            DBServerProvider.DbContext.SaveChanges();
            return base.GetPageData(options);
        }
        public override WebResponseContent Import(List<IFormFile> files)
        {
            //excel导入的时候写入主表id
            Guid ?ls_project_id = new Guid();
            ls_project_id=VOL.Core.Utilities.HttpContext.Current.Request("ls_project_id").ToGuid();
            WebResponseContent content = new WebResponseContent();
            ImportOnExecuting = (List<Ls_pro_bom> list) =>
            {
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].ls_project_id = (Guid)ls_project_id;
                }
                return content.OK();
            };
            return base.Import(files);
        }
        
  }
}
